package q55_canJump;

public class Solution_1 {
    public static void main(String[] args) {
        Solution_1 s = new Solution_1();
        int[] nums = {1,1,2,2,0,1,1};
        boolean a = s.canJump(nums);
        System.out.println(a);
    }

    public boolean canJump(int[] nums) {

        int[] res = MaxLengths(0,nums);

        while (res[1] < nums.length - 1) {
            int bin = res[1];
            res = MaxLengths(res[0],nums);
            if (bin == res[1]) {
                return false;
            }

        }

        return true;
    }
    public int[] MaxLengths(int index, int[] nums) {
        int max = 0;
        int[] res = new int[2];
        for (int i = 0; (i <= nums[index]) && (index + i) < nums.length; i++) {
            int bin = index + i  + nums[index + i];

            if (bin >= nums.length){
                return new int[] {i, bin};
            }

            if (bin >= max){
                res[0] = index + i;
                res[1] = bin;
                max = bin;
            }
            System.out.println(index + i  + nums[index + i]);
        }

        return res;
    }
}
